package com.zuo.demo01.controller.admin;


import com.alibaba.fastjson.JSON;
import com.zuo.demo01.pojo.Tag;
import com.zuo.demo01.service.TagService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.*;

@Controller
public class TagsController {


    @Autowired
    private TagService tagService;

    @RequestMapping("/table/list/tags")
    @ResponseBody
    public Map<String,Object> tags_list(@RequestParam("page") Integer page, @RequestParam("limit") Integer limit){

        /*if(page == null || limit == null){
            page = 1;
            limit = 10;
        }*/

        Map<String,Integer> map1 = new HashMap<>();
        map1.put("startIndex",(page-1)*limit);
        map1.put("pageSize",limit);

        List<Tag> list = tagService.findAll(map1);

        Map<String,Object> map2=new HashMap<>();
        map2.put("code",0);
        map2.put("msg","");
        map2.put("count",tagService.count());
        map2.put("data",list);
        return map2;
    }

    @RequestMapping("/tag/insert")
    @ResponseBody
    public String tag_insert(@RequestParam Map<String,Object> map) {
        Tag tag = new Tag((String) map.get("name"));
        int result = tagService.insert(tag);
        if(result==1){
            return "success";
        }else{
            return "已存在该标签";
        }
    }

    @RequestMapping("/tag/delete")
    @ResponseBody
    public String tag_delete(@RequestBody List<Tag> tags){
        Map<String,String> map = new HashMap<>();

        int result=0;
        for (Tag tag :
                tags) {
            result = tagService.delete(tag.getId());
            if(result!=1){
                map.put("status","doge");
                break;
            }
        }
        if(result==1){
            map.put("status","success");
        }

        String remap = JSON.toJSONString(map);
        return remap;
    }





}
